/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.spms.source.controller;

import com.wisnu.spms.service.entity.DbBilling;
import com.wisnu.spms.service.entity.DbBillingtype;
import com.wisnu.spms.service.entity.DbStudent;
import com.wisnu.spms.source.model.PanelBillingModel;
import com.wisnu.spms.source.view.PanelBilling;
import com.wisnu.spms.source.view.PanelBillingTypeList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Wisnu Wardoyo
 */
public class PanelBillingController {

    private final PanelBillingModel model;
    private final DbStudent student;

    public PanelBillingController(PanelBillingModel model, DbStudent student) {
        this.model = model;
        this.student = student;
    }

    public void changeSemester(PanelBilling panelBilling) {
        if (panelBilling.getComSemester().getSelectedIndex() != 0) {
            int order = panelBilling.getComSemester().getSelectedIndex();
            model.setStudent(student);
            model.setSemester(order);
            try {
                model.getDbBillings();
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(panelBilling, "Tidak dapat menampilkan billing");
            }
        }
    }

    public void addBillingType(PanelBilling panelBilling) {
        try {
            PanelBillingTypeList panelBillingTypeList = new PanelBillingTypeList(model.getBillingtypes());
            if (JOptionPane.showConfirmDialog(panelBilling, panelBillingTypeList,
                    "Daftar Billing Untuk Ditambahkan",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
                if (panelBillingTypeList.getBillingtype() != null) {
                    model.setBillingtype(panelBillingTypeList.getBillingtype());
                    Iterator<DbBilling> iterator = model.getList().iterator();
                    while (iterator.hasNext()) {
                        if (iterator.next().getDbBillingtype().getBillingTypeId() == model.getBillingtype().getBillingTypeId()) {
                            JOptionPane.showMessageDialog(panelBilling, "Billing Sudah Ada untuk semester ini");
                            model.setBillingtype(null);
                            return;
                        }
                    }
                    try {
                        model.insertBilling();
                        model.getDbBillings();
                    } catch (Exception e) {
                        JOptionPane.showMessageDialog(panelBilling, "Penambahan Gagal");
                    }
                } else {
                    JOptionPane.showMessageDialog(panelBilling, "Anda Belum Memilih Billing Type");
                }
            }
        } catch (Exception ex) {

        }

    }

}
